package org.sam.swing.table.tools;

import java.awt.BorderLayout;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXTable;
import org.sam.swing.JSPanel;
import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableColumnModel;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.JSTableModelEvent;
import org.sam.swing.table.JSTableModelLinster;
import org.sam.swing.table.defaultImpl.JSTableDefaultBuilderImpl;
import org.sam.swing.table.defaultImpl.JSTableModelDefaultLinster;
import org.sam.swing.table.editor.JSTableComboboxEditor;
import org.sam.swing.table.model.FilterObject;
import org.sam.swing.table.model.FilterOperation;
import org.sam.swing.table.renderer.JSTableMapRender;
import org.sam.swing.table.renderer.JSTableRowNumberRenderer;

/**
 * 过滤条件设置窗口
 * 
 * @author sam
 *
 */
public class JPanelFilter extends JSPanel implements FilterOperation, JSTableModelDefaultLinster<FilterObject> {

	private static final long serialVersionUID = 971471586891630890L;

	/**
	 * 当前绑定的操作table对象
	 */
	protected JXTable table;

	/**
	 * 当前被操作的tablemodel
	 */
	protected TableModel tableModel;

	/**
	 * 当前被操作的columnModel
	 */
	protected TableColumnModel colModel;

	/**
	 * 当前绑定的操作table对象
	 * 
	 * @return
	 */
	public JXTable getTable() {
		return table;
	}

	/**
	 * 当前绑定的操作table对象
	 * 
	 * @param table
	 */
	public void setTable(JXTable table) {
		this.table = table;
	}

	/**
	 * 当前被操作的tablemodel
	 * 
	 * @return
	 */
	public TableModel getTableModel() {
		return tableModel;
	}

	/**
	 * 当前被操作的tablemodel
	 * 
	 * @param tableModel
	 */
	public void setTableModel(TableModel tableModel) {
		this.tableModel = tableModel;
	}

	/**
	 * 当前被操作的columnModel
	 * 
	 * @return
	 */
	public TableColumnModel getColModel() {
		return colModel;
	}

	/**
	 * 当前被操作的columnModel
	 * 
	 * @param colModel
	 */
	public void setColModel(TableColumnModel colModel) {
		this.colModel = colModel;
	}
	
	/**
	 * 初始化过滤条件设置窗口
	 */
	public JPanelFilter(JXTable table, TableModel tableModel, TableColumnModel colModel) {
		this(table, tableModel, colModel, null);
	}

	/**
	 * 带初始化条件的过滤条件设置窗口
	 * 
	 * @param filterObjects
	 */
	public JPanelFilter(JXTable table, TableModel tableModel, TableColumnModel colModel,
			List<FilterObject> filterObjects) {
		this.setTable(table);
		this.setTableModel(tableModel);
		this.setColModel(colModel);
		this.filters = filterObjects;
		this.initCompents();
	}

	/**
	 * 当前操作的table对象
	 */
	protected JSTable filterTable;

	/**
	 * 当前操作的过滤表格tableModle
	 */
	protected JSTableModel<Collection<FilterObject>> filterTableModel;

	/**
	 * 当前操作的过滤表格columnModel对象
	 */
	protected JSTableColumnModel filterColumnModel;

	/**
	 * 过滤的数据
	 */
	protected List<FilterObject> filters;

	/**
	 * 条件表格
	 * @return
	 */
	public JSTable getFilterTable() {
		return filterTable;
	}

	/**
	 * 条件表格
	 * @param filterTable
	 */
	public void setFilterTable(JSTable filterTable) {
		this.filterTable = filterTable;
	}

	/**
	 * 条件表格model
	 * @return
	 */
	public JSTableModel<Collection<FilterObject>> getFilterTableModel() {
		return filterTableModel;
	}

	/**
	 * 条件表格model
	 * @param filterTableModel
	 */
	public void setFilterTableModel(JSTableModel<Collection<FilterObject>> filterTableModel) {
		this.filterTableModel = filterTableModel;
	}

	/**
	 * 条件表格列model
	 * @return
	 */
	public JSTableColumnModel getFilterColumnModel() {
		return filterColumnModel;
	}

	/**
	 * 条件表格列model
	 * @param filterColumnModel
	 */
	public void setFilterColumnModel(JSTableColumnModel filterColumnModel) {
		this.filterColumnModel = filterColumnModel;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<FilterObject> getFilterObjects() {
		return this.filters;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setFilterObjects(List<FilterObject> fitlers) {
		this.filters = fitlers;
		try {
			filterTableModel.retrieve();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化控件显示
	 * 
	 * @return
	 */
	protected JPanel initCompents() {
		this.setLayout(new BorderLayout());

		DefaultTableCellRenderer renderl = new DefaultTableCellRenderer();
		renderl.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);
		DefaultTableCellRenderer renderr = new DefaultTableCellRenderer();
		renderr.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);

		JSTableColumn c0 = new JSTableColumn();
		c0.setHeaderValue("#");
		c0.setTitle("#");
		c0.setIdentifier("");
		c0.setCellRenderer(new JSTableRowNumberRenderer());
		c0.setEditable(false);
		c0.setMaxWidth(35);

		JSTableColumn c1 = new JSTableColumn();
		c1.setHeaderValue("并且/或者");
		c1.setTitle("并且/或者");
		c1.setIdentifier("connectCondition");
		c1.setCellRenderer(renderl);
		c1.setDefaultValue(EMPTY);
		c1.setCellEditor(new DefaultCellEditor(new JComboBox<String>(new String[] { EMPTY, AND, OR })));
		c1.setMaxWidth(75);

		JSTableColumn c2 = new JSTableColumn();
		c2.setHeaderValue("");
		c2.setTitle("");
		c2.setIdentifier("leftBrackets");
		c2.setCellRenderer(renderl);
		c2.setDefaultValue(EMPTY);
		c2.setCellEditor(new DefaultCellEditor(new JComboBox<String>(
				new String[] { EMPTY, LEFT_ONE_BRACKETS, LEFT_TWO_BRACKETS, LEFT_THREE_BRACKETS })));
		c2.setMaxWidth(35);

		// 显示字段
		Map<String, String> columns = FilterOperation.getColumnNamesMapWithDisplay(this.getColModel());
		JSTableColumn c3 = new JSTableColumn();
		c3.setHeaderValue("字段");
		c3.setTitle("字段");
		c3.setIdentifier("filed");
		c3.setCellRenderer(new JSTableMapRender<>(columns));
		c3.setDefaultValue("");
		c3.setCellEditor(new JSTableComboboxEditor<>(columns));
		c3.setMinWidth(75);

		JSTableColumn c4 = new JSTableColumn();
		c4.setHeaderValue("条件");
		c4.setTitle("条件");
		c4.setIdentifier("condition");
		c4.setCellRenderer(renderl);
		c4.setDefaultValue(EMPTY);
		c4.setCellEditor(new DefaultCellEditor(
				new JComboBox<String>(new String[] { CONTAINS, EQUAL, NOTEQUAL, MORETHAN, LESSTHAN, IN })));
		c4.setMaxWidth(55);

		JSTableColumn c5 = new JSTableColumn();
		c5.setHeaderValue("值");
		c5.setTitle("值");
		c5.setIdentifier("patternValue");
		c5.setCellRenderer(renderl);
		c5.setDefaultValue(EMPTY);
		c5.setMinWidth(175);

		JSTableColumn c6 = new JSTableColumn();
		c6.setHeaderValue("");
		c6.setTitle("");
		c6.setIdentifier("rightBrackets");
		c6.setCellRenderer(renderl);
		c6.setDefaultValue(EMPTY);
		c6.setCellEditor(new DefaultCellEditor(new JComboBox<String>(
				new String[] { EMPTY, RIGHT_ONE_BRACKETS, RIGHT_TWO_BRACKETS, RIGHT_THREE_BRACKETS })));
		c6.setMaxWidth(35);

		try {
			/**
			 * 重新绑定本类为数据实现类
			 */
			JSTableBuilder<Collection<FilterObject>> builder = new JSTableDefaultBuilderImpl<FilterObject>(
					FilterObject.class, c0, c1, c2, c3, c4, c5, c6) {

				@Override
				public JSTableModelLinster<Collection<FilterObject>> buildModelLinster(
						JSTableModel<Collection<FilterObject>> tableModel) throws Exception {
					tableModel.setTableModelLinster(JPanelFilter.this);
					return JPanelFilter.this;
				}
			};

			filterTableModel = builder.buildTableModel();
			filterColumnModel = builder.buildTableColumnModel();
			filterTable = builder.buildTable();
			filterTableModel.retrieve();

		} catch (Exception e) {
			e.printStackTrace();
		}

		JScrollPane scrollPane = new JScrollPane(filterTable);
		this.add(scrollPane, BorderLayout.CENTER);

		// 以下是测试表格显示风格的操作
		JSTableToolBar toolBar = new JSTableToolBar(filterTable);
		toolBar.enableEdit();
		toolBar.enableAdd();
		toolBar.enableDel();
		toolBar.enableSave();
		add(toolBar, BorderLayout.NORTH);

		return this;
	}

	/**
	 * {@inheritDoc} 检索数据操作
	 */
	@Override
	public Collection<FilterObject> onRetrieve() throws Exception {
		return this.filters;
	}

	/**
	 * 提交数据到过滤条件对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean update(JSTableModelEvent event) throws Exception {
		this.setFilterObjects((List<FilterObject>)event.getTableModel().getDatas());
		return true;
	}

}
